/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.reservacion.vista;

import basedatos.OperacionesBD;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import modulos.usuario.modelo.Usuario;
import modulos.reservacion.modelo.Hora;
import modulos.reservacion.modelo.Reservacion_Config;
import vista.frmPrincipal;
import vista.modelo.OperacionesVarias;

/**
 *
 * @author Edulima
 */
public class iFrmReservaCitaConfig extends javax.swing.JInternalFrame {

    private Usuario usuario;
    private Reservacion_Config reservacion_Config;
    private ArrayList<Hora> duracionCitas = new ArrayList<Hora>();
    private ArrayList<Hora> horaAm;
    private ArrayList<Hora> horaPm;
    private ArrayList<Usuario> usuarios = new ArrayList<Usuario>();
    private ArrayList<Object> reservaciones = new ArrayList<Object>();

    /**
     * Creates new form iFrmReservaCita1
     */
    public iFrmReservaCitaConfig() {
        initComponents();
        OperacionesVarias.cerrarVentanaEscape(this);
        iniciarOpciones();
        usuario = (Usuario) frmPrincipal.UsuarioLogueado.get(0)[1];
        if (usuario.getCuentaUsr().getTipo().equalsIgnoreCase("ADMINISTRADOR")) {
            iniciarAdmin();
        } else {
            iniciarMedico();
        }

        this.setVisible(true);
    }

    private void iniciarAdmin() {
        cmbUsuarios.removeAllItems();
        reservaciones = (ArrayList<Object>) OperacionesBD.listar("Reservacion_Config");
        for (Object us : reservaciones) {
            cmbUsuarios.addItem(((Reservacion_Config) us).getUsuario());
        }
    }

    private void iniciarMedico() {
        pnlMedico.removeAll();
        txtMedico.setText(usuario + "");
        pnlMedico.add(txtMedico);
        reservacion_Config = (Reservacion_Config) OperacionesBD.buscar("Reservacion_Config", "usuario_idPersona", "" + usuario.getIdPersona());
        if (reservacion_Config == null) {
            OperacionesBD.guardar(new Reservacion_Config(usuario));
            reservacion_Config = (Reservacion_Config) OperacionesBD.buscar("Reservacion_Config", "usuario_idPersona", "" + usuario.getIdPersona());
        }

        setConfiguracion(reservacion_Config);
    }

    private void setConfiguracion(Reservacion_Config reservacion) {
        int dura = 0, aam = 0, apm = 0, dam = 0, dpm = 0;
        for (int i = 0; i < duracionCitas.size(); i++) {
            if (duracionCitas.get(i).getTiempo() == reservacion.getDuracion()) {
                dura = i;
                break;
            }
        }


        for (int i = 0; i < horaAm.size(); i++) {
            if (horaAm.get(i).getTiempo() == reservacion.getHorarioAmDesde()) {
                dam = i;
            }
            if (horaAm.get(i).getTiempo() == reservacion.getHorarioAmHasta()) {
                aam = i;
            }
        }
        for (int i = 0; i < horaPm.size(); i++) {
            if (horaPm.get(i).getTiempo() == reservacion.getHorarioPmdesde()) {
                dpm = i;
            }
            if (horaPm.get(i).getTiempo() == reservacion.getHorarioPmHasta()) {
                apm = i;
            }
        }

        cmbDuracionCita.setSelectedIndex(dura);

        cmbAAm.setSelectedIndex(aam);
        cmbAPm.setSelectedIndex(apm);
        cmbDeAm.setSelectedIndex(dam);
        cmbDePm.setSelectedIndex(dpm);
    }

    private void iniciarOpciones() {
        duracionCitas = new ArrayList<Hora>();
        duracionCitas.add(new Hora(10));
        duracionCitas.add(new Hora(15));
        duracionCitas.add(new Hora(30));
        duracionCitas.add(new Hora(45));
        duracionCitas.add(new Hora(60));
        duracionCitas.add(new Hora(120));
        for (Hora h : duracionCitas) {
            cmbDuracionCita.addItem(h.hora());
        }

        horaAm = new ArrayList<Hora>();
        for (int i = 7; i < 14; i++) {
            horaAm.add(new Hora(i, 0));
            horaAm.add(new Hora(i, 30));
        }
        for (Hora h : horaAm) {
            cmbDeAm.addItem(h);
            cmbAAm.addItem(h);
        }

        horaPm = new ArrayList<Hora>();
        for (int i = 14; i < 21; i++) {
            horaPm.add(new Hora(i, 0));
            horaPm.add(new Hora(i, 30));
        }
        for (Hora h : horaPm) {
            cmbDePm.addItem(h);
            cmbAPm.addItem(h);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtMedico = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        pnlId1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        cmbDuracionCita = new javax.swing.JComboBox();
        cmbDeAm = new javax.swing.JComboBox();
        cmbDePm = new javax.swing.JComboBox();
        cmbAAm = new javax.swing.JComboBox();
        cmbAPm = new javax.swing.JComboBox();
        jButton2 = new javax.swing.JButton();
        pnlId2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        pnlMedico = new javax.swing.JPanel();
        cmbUsuarios = new javax.swing.JComboBox();
        pnlId3 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();

        txtMedico.setEditable(false);
        txtMedico.setText("jTextField1");

        setClosable(true);
        setTitle("Configuraciones de las Reservaciones");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/karm.png"))); // NOI18N

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        jButton1.setText("Cerrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        pnlId1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP, null, null));

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        jLabel1.setText("Duracion de las Citas:");

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        jLabel2.setText("Horario de Atencion:");

        jLabel4.setFont(new java.awt.Font("Waree", 3, 13)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(30, 12, 142));
        jLabel4.setText("Mañana");

        jLabel5.setFont(new java.awt.Font("Waree", 3, 13)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(30, 12, 142));
        jLabel5.setText("Tarde");

        jLabel6.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        jLabel6.setText("A:");

        jLabel7.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        jLabel7.setText("De:");

        jLabel8.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        jLabel8.setText("De:");

        jLabel9.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        jLabel9.setText("A:");

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/filesave.png"))); // NOI18N
        jButton2.setText("Guardar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlId1Layout = new javax.swing.GroupLayout(pnlId1);
        pnlId1.setLayout(pnlId1Layout);
        pnlId1Layout.setHorizontalGroup(
            pnlId1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlId1Layout.createSequentialGroup()
                .addGroup(pnlId1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlId1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(pnlId1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlId1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(46, 46, 46)
                                .addComponent(cmbDuracionCita, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel2)
                            .addGroup(pnlId1Layout.createSequentialGroup()
                                .addGap(199, 199, 199)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(pnlId1Layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addGroup(pnlId1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(71, 71, 71)
                        .addGroup(pnlId1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlId1Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(18, 18, 18)
                                .addComponent(cmbDePm, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnlId1Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addGap(18, 18, 18)
                                .addComponent(cmbDeAm, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(53, 53, 53)
                        .addGroup(pnlId1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel9))
                        .addGap(26, 26, 26)
                        .addGroup(pnlId1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cmbAAm, 0, 89, Short.MAX_VALUE)
                            .addComponent(cmbAPm, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(85, Short.MAX_VALUE))
        );
        pnlId1Layout.setVerticalGroup(
            pnlId1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlId1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlId1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cmbDuracionCita, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(52, 52, 52)
                .addComponent(jLabel2)
                .addGap(19, 19, 19)
                .addGroup(pnlId1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(cmbDeAm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbAAm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(pnlId1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(cmbDePm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbAPm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addContainerGap())
        );

        pnlId2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP, null, null));

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        jLabel3.setText("Nombre del Medico:");

        pnlMedico.setLayout(new java.awt.BorderLayout());

        cmbUsuarios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbUsuariosActionPerformed(evt);
            }
        });
        pnlMedico.add(cmbUsuarios, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout pnlId2Layout = new javax.swing.GroupLayout(pnlId2);
        pnlId2.setLayout(pnlId2Layout);
        pnlId2Layout.setHorizontalGroup(
            pnlId2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlId2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addGap(59, 59, 59)
                .addComponent(pnlMedico, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnlId2Layout.setVerticalGroup(
            pnlId2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlId2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlId2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlMedico, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap())
        );

        pnlId3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP, null, null));

        jButton4.setText("Servicios");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlId3Layout = new javax.swing.GroupLayout(pnlId3);
        pnlId3.setLayout(pnlId3Layout);
        pnlId3Layout.setHorizontalGroup(
            pnlId3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlId3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton4)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlId3Layout.setVerticalGroup(
            pnlId3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlId3Layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(jButton4)
                .addContainerGap(50, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(pnlId2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(pnlId1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(pnlId3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(246, 246, 246)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(pnlId2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlId1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlId3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        iFrmServiciosAdmin FrCita11 = new iFrmServiciosAdmin(reservacion_Config.getUsuario());
        frmPrincipal.dskpPrincipal.add(FrCita11);
        FrCita11.toFront();
        FrCita11.setVisible(true);
        OperacionesVarias.centrar(FrCita11);

    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        reservacion_Config.setDuracion(duracionCitas.get(cmbDuracionCita.getSelectedIndex()).getTiempo());
        reservacion_Config.setHorarioAmDesde(horaAm.get(cmbDeAm.getSelectedIndex()).getTiempo());
        reservacion_Config.setHorarioAmHasta(horaAm.get(cmbAAm.getSelectedIndex()).getTiempo());
        reservacion_Config.setHorarioPmHasta(horaPm.get(cmbAPm.getSelectedIndex()).getTiempo());
        reservacion_Config.setHorarioPmdesde(horaPm.get(cmbDePm.getSelectedIndex()).getTiempo());
        if (OperacionesBD.modificar(reservacion_Config)) {
            JOptionPane.showMessageDialog(this, "Configuracion guardada", "Configuracion - Reservacion", JOptionPane.INFORMATION_MESSAGE);
            iniciarAdmin();
        } else {
            JOptionPane.showMessageDialog(this, "La configuaracion no ha podido ser actualizada", "Configuracion - Reservacion", JOptionPane.ERROR_MESSAGE);

        }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void cmbUsuariosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbUsuariosActionPerformed
        int index = cmbUsuarios.getSelectedIndex();
        if (index != -1) {
            reservacion_Config = (Reservacion_Config) reservaciones.get(index);
            setConfiguracion(reservacion_Config);
        }


    }//GEN-LAST:event_cmbUsuariosActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbAAm;
    private javax.swing.JComboBox cmbAPm;
    private javax.swing.JComboBox cmbDeAm;
    private javax.swing.JComboBox cmbDePm;
    private javax.swing.JComboBox cmbDuracionCita;
    private javax.swing.JComboBox cmbUsuarios;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel pnlId1;
    private javax.swing.JPanel pnlId2;
    private javax.swing.JPanel pnlId3;
    private javax.swing.JPanel pnlMedico;
    private javax.swing.JTextField txtMedico;
    // End of variables declaration//GEN-END:variables
}
